
package mx.itschapala.sdw2.vista;

import java.awt.event.ActionEvent;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import mx.itschapala.dsw2.biblioteca8a.bl.AutorBlLocal;
import mx.itschapala.dsw2.biblioteca8a.modelo.Autor;


    
    ///-----Listeners-------

   

   
@ManagedBean
@SessionScoped
public class AuthorBean {
    
    
    //----- EJB'S------------
    @EJB
    private AutorBlLocal autorBl;
   
    
    
    ///----- variables(privadas)-------
    private List<Autor>Lista;
     private Autor author;
     private String accion;
    
    //------ constructor-------
    public AuthorBean() {
        author = new Autor();
    }
    //------ metodos  Get  Y  SET-------
    
     public List<Autor> getLista() {
         Lista = autorBl.getAll();
        return Lista;
    }
    


    public Autor getAuthor() {
        return author;
    }

    public void setAuthor(Autor author) {
        this.author = author;
    }
    
    
    //--------- acciones-------
    public String proccesarAccion(){
        autorBl.register(author);
//    return"AutorLista";
         return"Autor";
    }
    public String LimpiarCampos(){
        author = new Autor();
    return "AutorCreaaEditar";
    
    }
    
      public String procesarAccion(){
        if(accion.equalsIgnoreCase("Nuevo")){
        autorBl.register(author);  
        }else{
        if(accion.equalsIgnoreCase("Editar")){
        autorBl.register(author);
        }
    }
        accion ="";
      return "AutorMenu";
      
      }
    //-----LISTENER -----
    
    public void prepararNuevo(ActionEvent evt){
    author = new Autor();
    }
    
    public void prepararEditar(ActionEvent evt){
     System.out.println(evt.getComponent().getClientId());
     String arr[]=evt.getComponent().getClientId();
      String num= arr[2];
       int index = Integer.parseInt(num);
           
    System.out.println("El numero es:" + index);
    author = Lista.get(index);
    
    accion = "Editar";
    
    }
}

